package priv.abadstring.study.haoke.webapi.manage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import priv.abadstring.study.haoke.webapi.mongo.House;
import priv.abadstring.study.haoke.webapi.vo.map.MapHouseDataResult;
import priv.abadstring.study.haoke.webapi.vo.map.MapHouseXY;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * HouseLocationManageByMongo
 *
 * @author aBadString
 * @date 2021-08-05 18:14:04
 */
@Service
public class HouseLocationManageByMongo {
    /**
     * 地图缩放比和比例尺的对应关系
     */
    public static final Map<Integer, Double> BAIDU_ZOOM = new HashMap<>(19);
    static {
        // 单位为 km
        BAIDU_ZOOM.put(19, 20d / 1000);
        BAIDU_ZOOM.put(18, 50d / 1000);
        BAIDU_ZOOM.put(17, 100d / 1000);
        BAIDU_ZOOM.put(16, 200d / 1000);
        BAIDU_ZOOM.put(15, 500d / 1000);
        BAIDU_ZOOM.put(14, 1d);
        BAIDU_ZOOM.put(13, 2d);
        BAIDU_ZOOM.put(12, 5d);
        BAIDU_ZOOM.put(11, 10d);
        BAIDU_ZOOM.put(10, 20d);
        BAIDU_ZOOM.put(9, 25d);
        BAIDU_ZOOM.put(8, 50d);
        BAIDU_ZOOM.put(7, 100d);
        BAIDU_ZOOM.put(6, 200d);
        BAIDU_ZOOM.put(5, 500d);
        BAIDU_ZOOM.put(4, 1000d);
        BAIDU_ZOOM.put(3, 2000d);
        BAIDU_ZOOM.put(2, 5000d);
        BAIDU_ZOOM.put(1, 10000d);
    }

    @Autowired
    private MongoTemplate mongoTemplate;

    public MapHouseDataResult queryHouseData(Double lng, Double lat, Integer zoom) {
        double distance = BAIDU_ZOOM.get(zoom) * 1.5 / 111.12; // 1.5 倍距离范围, 根据实际需求调整

        // db.house.find({loc:{$near:[lng,lat],$maxDistance:distance}})
        Query query = Query.query(Criteria.where("loc")
                .near(new Point(lng, lat))
                .maxDistance(distance));
        List<House> mongoHouses = this.mongoTemplate.find(query, House.class);

        // 将房子的经纬度选择出来
        List<MapHouseXY> list = new ArrayList<>(mongoHouses.size());
        for (House mongoHouse : mongoHouses) {
            list.add(new MapHouseXY(mongoHouse.getLoc()[0], mongoHouse.getLoc()[1]));
        }
        return new MapHouseDataResult(list);
    }
}
